/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/4/6  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.ResponseDO;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.Enum.BehaviorEnum;
import com.sansi.pencilbook.Enum.RelationEnum;
import com.sansi.pencilbook.entity.User;
import com.sansi.pencilbook.entity.UserBookRelation;
import com.sansi.pencilbook.service.UserBookRelationServer;
import com.sansi.pencilbook.service.UserBookServer;
import com.sansi.pencilbook.service.UserServer;
import com.sansi.pencilbook.util.ResponseDOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能说明: 用户和书籍关系控制器
 *
 * @author lisy31662
 */
@Slf4j
@RestController
@RequestMapping("/api/userBookRelation")
public class RelationController extends VerifyController implements BaseController {

  @Autowired
  private UserBookRelationServer userBookRelationServer;

  @Autowired
  private UserServer userServer;

  @Autowired
  private UserBookServer userBookServer;

  @PostMapping("/u/addUserBookRelation")
  public ResponseDO addUserBookRelation(@RequestBody UserBookRelation userBookRelation, HttpServletRequest request) {
    String userAccount = "";
    if (!"".equals((userAccount=getUserAccount(request))) || checkAdmin(request)) {
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      userBookRelation.setUserId(user.getUserId());
      if (RelationEnum.BLACK.getRelation().equals(userBookRelation.getRelation())) {
        userBookServer.addUserBook(BehaviorEnum.BLACKLIST, userBookRelation.getBookIsbn(), user.getUserId());
      }else if(RelationEnum.COLLECT.getRelation().equals(userBookRelation.getRelation())) {
        userBookServer.addUserBook(BehaviorEnum.COLLECT, userBookRelation.getBookIsbn(), user.getUserId());
      }else if(RelationEnum.CART.getRelation().equals(userBookRelation.getRelation())) {
        userBookServer.addUserBook(BehaviorEnum.ADDCART, userBookRelation.getBookIsbn(), user.getUserId());
      }else {
        return ResponseDOUtil.setResponseDO(false, "关系错误");
      }
      Result result = userBookRelationServer.addUserBookRelation(userBookRelation);
      return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登陆后再操作");
    }
  }

  @PostMapping("/u/getRelationByUserBookRelation")
  public ResponseDO getRelationByUserBookRelation(@RequestParam("isbn") String isbn, @RequestParam("relation") String relation,HttpServletRequest request) {
    String userAccount = "";
    if (!"".equals((userAccount=getUserAccount(request))) || checkAdmin(request)) {
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      Result result = userBookRelationServer.getUserBookRelationByUserBookRelation(user.getUserId(), isbn, RelationEnum.valueOf(relation));
      return getResponseDO(result);
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登陆后再操作");
    }
  }

  @PostMapping("/u/getRelationByUserRelation")
  public ResponseDO getRelationByUserRelation(@RequestParam("relation") String relation, HttpServletRequest request) {
    String userAccount = "";
    if (!"".equals((userAccount=getUserAccount(request))) || checkAdmin(request)) {
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      Result result = userBookRelationServer.getUserBookRelationByUserRelation(user.getUserId(), RelationEnum.valueOf(relation));
      return getResponseDO(result);
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登陆后再操作");
    }
  }

  @PostMapping("/u/modifyRelation")
  public ResponseDO modifyRelation(@RequestBody UserBookRelation userBookRelation,HttpServletRequest request) {
    String userAccount = "";
    if (!"".equals((userAccount=getUserAccount(request))) || checkAdmin(request)) {
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      userBookRelation.setUserId(user.getUserId());
      Result result = userBookRelationServer.modifyUserBookRelation(userBookRelation);
      return getResponseDO(result);
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登陆后再操作");
    }
  }

  @PostMapping("/u/deleteRelation")
  public ResponseDO deleteRelation(@RequestBody UserBookRelation userBookRelation,HttpServletRequest request) {
    String userAccount = "";
    if (!"".equals((userAccount=getUserAccount(request))) || checkAdmin(request)) {
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      userBookRelation.setUserId(user.getUserId());
      if (RelationEnum.BLACK.getRelation().equals(userBookRelation.getRelation())) {
        userBookServer.addUserBook(BehaviorEnum.UNBLACKLIST, userBookRelation.getBookIsbn(), user.getUserId());
      }else if(RelationEnum.COLLECT.getRelation().equals(userBookRelation.getRelation())) {
        userBookServer.addUserBook(BehaviorEnum.UNCOLLECTED, userBookRelation.getBookIsbn(), user.getUserId());
      }else if(RelationEnum.CART.getRelation().equals(userBookRelation.getRelation())) {
        userBookServer.addUserBook(BehaviorEnum.UNCART, userBookRelation.getBookIsbn(), user.getUserId());
      }else {
        return ResponseDOUtil.setResponseDO(false, "关系错误");
      }
      Result result = userBookRelationServer.deleteUserBookRelation(userBookRelation);
      return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登陆后再操作");
    }
  }

  private ResponseDO getResponseDO(Result result)  {
    if (result.isSuccess()) {
      if (result.getEffectRow() > 0) {
        return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData());
      }else {
        return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg(), result.getData());
      }
    }else {
      return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }
  }

}
